package com.quik.web.token.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.SysAccount;
import com.quik.entity.DtaToken;
import com.quik.exception.ErrorCode;
import com.quik.web.token.service.TokenService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明：
 *      手牌管理的业务控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/token")
public class TokenController {
	private static Logger log = (Logger)Logger.getLogger(TokenController.class);
	
	@Autowired
	private TokenService tokenService;
	
	/**
	 * 进入手牌管理界面
	 * @return
	 */
	@RequestMapping(value = "tokenList", method = RequestMethod.GET)
	public ModelAndView tokenList(){
		ModelAndView mav = new ModelAndView("/token/tokenList");
		
		mav.addObject("tokenStatusFree", DtaToken.TOKEN_STATUS_FREE);
		
		return mav;
	}
	
	/**
	 * 查询手牌
	 * @param page
	 * @param rows
	 * @param tokenNo
	 * @param tokenTypeName
	 * @return
	 */
	@RequestMapping(value = "queryToken", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryToken(Integer page, Integer rows, String tokenNo, String tokenTypeName){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = tokenService.findTokenCount(tokenNo, tokenTypeName);
		    List list = tokenService.findToken(tokenNo, tokenTypeName, page, rows);
		    
		    dto.setRows(list);
		    dto.setTotal(totalCount);
		    
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenController queryToken error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 新增手牌界面
	 * @return
	 */
	@RequestMapping(value = "initAddToken")
	public ModelAndView initAddToken(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/token/addToken");
		String result = (String)request.getAttribute("result");
		
		if(result != null && result.length() > 0)
			mav.addObject("result", result);
		
		mav.addObject("tokenFreeStatus", DtaToken.TOKEN_STATUS_FREE);
		mav.addObject("tokenUsedStatus", DtaToken.TOKEN_STATUS_USED);
		
		return mav;
	}
	
	/**
	 * 检查手牌编号是否重复
	 * @param tokenNo
	 * @return
	 */
	@RequestMapping(value = "checkTokenNo", method = RequestMethod.POST)
	public ResponseEntity<String> checkTokenNo(String tokenNo){
		boolean flag = false;
		
		try{
			
			flag = tokenService.checkTokenNo(tokenNo);
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenController checkTokenNo error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 新增手牌
	 * @param token
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addToken", method = RequestMethod.POST)
	public ModelAndView addToken(DtaToken token, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/token/addToken");
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		String result = ErrorCode.RESULT_ADD_TOKEN_FAILED;
		
		try{
			token.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			token.setCrt_user(account.getStaff_no());
			
			tokenService.addToken(token);
			result = ErrorCode.RESULT_ADD_TOKEN_SUCCESS;
			
			mav.addObject("tokenFreeStatus", DtaToken.TOKEN_STATUS_FREE);
			mav.addObject("tokenUsedStatus", DtaToken.TOKEN_STATUS_USED);
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenController addToken error: " + e.getMessage());
		}
		
		mav.addObject("result", result);
		return mav;
	}
	
}
